import originJsonp from 'jsonp'

export function jsonp (url, data, option) {
  // url先要与data进行拼接
  url += (url.indexOf('?') > 0 ? '&' : '?') + param(data)
  return new Promise((resolve, reject) => {
    originJsonp(url, option, (err, data) => {
      if(!err) {
        resolve(data)
      }else{
        reject(err)
      }
    })
  })
}
function param(data) {
  let url = ''
  // 遍历data取出数据
  for (var k in data) {
    // 判断data[k]是不是underfined
    let value = data[k] !== undefined ? data[k] : ''
    url += '&' + k + '=' + encodeURIComponent(value)
  }
  return url ? url.substring(1) : ''
}